package com.mareas.web.jsf.view;
 
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.imageio.ImageIO;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.richfaces.component.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mareas.web.model.integration.InformeProformaDTO;
import com.mareas.web.service.InformeProformaService;
 
@Component("informeProformaView")
@ManagedBean
@RequestScoped 
public class InformeProformaView implements Serializable {

	private static final long serialVersionUID = -2277626697118102318L;
 
	@ManagedProperty(value = "#{informeProformaService}")
	@Autowired
	private InformeProformaService informeProformaService;
    private SortOrder diferenciaCostosOrder = SortOrder.unsorted;
    private CategoryDataset dataset;;
    private JFreeChart grafico;

	public InformeProformaService getInformeProformaService() {
		return informeProformaService;
	}

	public void setInformeProformaService(
			InformeProformaService informeProformaService) {
		this.informeProformaService = informeProformaService;
	}

	public List<InformeProformaDTO> getProformas() {
		return this.informeProformaService.getListadoProforma();
	}

	public SortOrder getDiferenciaCostosOrder() {
		return diferenciaCostosOrder;
	}

	public void setDiferenciaCostosOrder(SortOrder diferenciaCostosOrder) {
		this.diferenciaCostosOrder = diferenciaCostosOrder;
	}

	public void ordenarPorDiferenciaDeCostos() {
        if (diferenciaCostosOrder.equals(SortOrder.ascending)) {
        	setDiferenciaCostosOrder(SortOrder.descending);
        } else {
        	setDiferenciaCostosOrder(SortOrder.ascending);
        }
    }
	
	
	
	public void generarGraficoProformas(OutputStream out, Object data) {
		this.dataset = this.crearDataset(); 
		this.grafico = this.crearGrafico(this.dataset);

	 
	    BufferedImage buffImg = this.grafico.createBufferedImage(
	                                500, //width
	                                375, //height
	                                BufferedImage.TYPE_INT_RGB, //image type
	                                null);
	 
	    try {
			ImageIO.write(buffImg, "jpeg", out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private JFreeChart crearGrafico(CategoryDataset dataset){
		// create the chart...
		JFreeChart chart = ChartFactory.createLineChart(
				"Evolucion", //titulo
				"Operacion", //label eje x
				"Difencia Costos", //label eje y
				dataset, // data
				PlotOrientation.VERTICAL, // orientacion
				false, // incluir leyenda
				true, // tooltips
				false // urls
				);
		chart.setBackgroundPaint(Color.white);
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setRangeGridlinePaint(Color.white);
		// customise the range axis...
		NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		// customise the renderer...
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		renderer.setShapesVisible(true);
		renderer.setDrawOutlines(true);
		renderer.setUseFillPaint(true);
		renderer.setFillPaint(Color.white);
		return chart;
		
	}
	

    private CategoryDataset crearDataset() {
    	List<InformeProformaDTO> listadoProformas;
    	listadoProformas = this.getProformas();
    	DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    	for (InformeProformaDTO informeProforma : listadoProformas) {
    		dataset.addValue(informeProforma.getDiferenciaCostos(), "Proforma", informeProforma.getProformaId());
			
		}
		return dataset;
        
    }
}